@{
    Layout = null;
}
<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>

<div class="layui-form" lay-filter="updateForm" style="padding: 20px;">
    <input type="hidden" name="id" id="equipmentId" />
    <div class="layui-form-item">
        <label class="layui-form-label">设备类型编码</label>
        <div class="layui-input-block">
            <input type="text" name="equipmentTypeCode" id="equipmentTypeCodeInput" required lay-verify="required" placeholder="请输入设备类型编码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">设备类型名称</label>
        <div class="layui-input-block">
            <input type="text" name="equipmentTypeName" required lay-verify="required" placeholder="请输入设备类型名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <select name="isEnable" required lay-verify="required">
                <option value="">请选择</option>
                <option value="是">是</option>
                <option value="否">否</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="notes" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit lay-filter="submitUpdate" id="saveBtn">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll()">取消</button>
    </div>
</div>

<script>
layui.use(['form', 'layer'], function(){
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.$;

    // 获取URL参数中的ID
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    // 页面加载时获取数据
    $(document).ready(function(){
        var id = getUrlParam('id');
        if(id){
            $('#equipmentId').val(id);
            // 获取设备类型详情
            $.ajax({
                url: '/EquipmentType/FanEquipmentType?equipmentTypeId=' + id,
                type: 'GET',
                success: function(data){
                    if(data){
                        form.val('updateForm', {
                            equipmentTypeCode: data.equipmentTypeCode,
                            equipmentTypeName: data.equipmentTypeName,
                            isEnable: data.isEnable,
                            notes: data.notes
                        });
                    }
                }
            });
        }
    });

    // 提交表单
    form.on('submit(submitUpdate)', function(data){
        var field = data.field;
        var $btn = $('#saveBtn');
        $btn.addClass('layui-btn-disabled').attr('disabled', true).text('保存中...');
        
        $.ajax({
            url: '/EquipmentType/UpdateEquipmentType',
            type: 'POST',
            data: {
                id: $('#equipmentId').val(),
                equipmentTypeCode: field.equipmentTypeCode,
                equipmentTypeName: field.equipmentTypeName,
                isEnable: field.isEnable,
                notes: field.notes
            },
            success: function(res){
                if(res > 0){
                    parent.layer.msg('保存成功');
                    parent.layer.closeAll();
                } else {
                    layer.msg('保存失败');
                }
            },
            error: function(){
                layer.msg('操作失败，请重试');
            },
            complete: function(){
                $btn.removeClass('layui-btn-disabled').attr('disabled', false).text('保存');
            }
        });
        return false;
    });
});
</script> 